package com.weng.coder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.coder.entity.Column;
import com.weng.coder.service.ColumnService;
import com.weng.framework.core.model.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 列 Web API
 *
 * @作者 wengzhonghui
 * @创建时间 2018-02-24 20:18:16
 */
@RestController
@RequestMapping("/column") 
@Api(value = "列相关接口")
public class ColumnController{
	@Resource
	private ColumnService columnService;
	
	
	 /**
	 * 获取分页数据
	 */
	 @ResponseBody
	 @RequestMapping(value="/list",method=RequestMethod.GET)
	 @ApiOperation(value = "分页获取列列表", notes = "分页获取列列表")
	 @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query",name = "pageNo",dataType ="Integer" ,required = true,value = "页数"),
        @ApiImplicitParam(paramType = "query",name = "pageSize",dataType ="Integer" ,required = true,value = "每页数量")
	 })
	 public ResponseData getDataByPage(Integer pageNo, Integer pageSize, String tableId
					 , String code, String name)throws Exception{
		 Column column = new Column();		 
		 column.setCode(code);
		 column.setTableId(tableId);
		 column.setRemarks(name);
		 IPage<Column> page = columnService.getDataByPage(pageNo, pageSize, column);
		 return ResponseData.success(page);
	 }
	
	 /**
	 * 获取列
	 * 
	 */
	@RequestMapping(value="get-one",method=RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取列", notes = "获取列")
	public ResponseData getColumn(String id, Model model
			,HttpServletRequest request) throws Exception{
		if(StringUtils.isEmpty(id)){
			return ResponseData.failed("列Id不能为空！");
		}
		
		Column column = this.columnService.getOne(id);
		return ResponseData.success(column);
	}
	 
	/**
	 * 添加列
	 * 
	 */
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加列", notes = "添加列")
	public ResponseData addColumn(Column column, Model model
			,HttpServletRequest request) throws Exception{
		if(column==null ){
			return ResponseData.failed("列不能为空！");
		}
		columnService.save(column);
		return ResponseData.success("添加列成功！");
	}
	
	/**
	 * 更新列
	 */
	@RequestMapping(value="update",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "更新列", notes = "更新列")
	public ResponseData updateColumn(Column column,HttpServletRequest request) throws Exception{
		if(column==null ){
			return ResponseData.failed("列不能为空！");
		}
		columnService.save(column);
		return ResponseData.success("更新成功！");
	}
	
	
	/**
	 * 删除列
	 */
	@RequestMapping(value="delete",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除列", notes = "删除列,多个ID以,号分隔")
	@Transactional
	public  ResponseData deletecolumn( String ids
			,HttpServletRequest request) throws Exception{
		String[] columnIdList = ids.split(",");
		if(columnIdList == null || columnIdList.length < 1){
			return ResponseData.failed("列ID不能为空");
		}
		StringBuilder logDesc = new StringBuilder();
		List<Column> columnList = columnService.findByIds(Arrays.asList(columnIdList));
		if(columnList!=null && columnList.size()>0){
			for(Column column:columnList){
				if(logDesc.length()>0){
					logDesc.append(",");
				}
				logDesc.append(column.getId());
				this.columnService.delete(column);
			}
		}
		return ResponseData.success("删除成功");
	}


}

